package at.fhj.itm;

import java.util.Date;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.TextView;
import android.widget.Toast;


public class TaskActivity extends Activity implements OnClickListener{
	
	//Button to save and delete a task
	private Button btnSave, btnDelete;
	//TextView to enter the headline and the description of the task
	private TextView enterTaskName, enterTaskDetail;
	private DatePicker datepicker;
	//DB-access
	private DBAccess dbAccess;
	
	
	public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.task);
        
        btnSave = (Button) findViewById(R.id.task_button_save);
        btnSave.setOnClickListener(this);
        btnDelete = (Button) findViewById(R.id.task_button_delete);
        btnDelete.setOnClickListener(this);
        
        enterTaskName = (TextView)findViewById(R.id.task_taskname);
        enterTaskName.setOnClickListener(this);
        enterTaskDetail = (TextView)findViewById(R.id.task_details);
        enterTaskDetail.setOnClickListener(this);
        
        datepicker = (DatePicker)findViewById(R.id.task_datepicker);
      
	}
        
	@Override
	public void onClick(View v) {
		if(v == btnSave) {
			if(!enterTaskName.getText().toString().equals("")) {
				String taskName = enterTaskName.getEditableText().toString();
				String details    = enterTaskDetail.getEditableText().toString();
				Date dueDate = new Date(datepicker.getYear() - 1900, datepicker.getMonth(), datepicker.getDayOfMonth());
				
				String sql = "CREATE TABLE freunde (_id INTEGER PRIMARY KEY AUTOINCREMENT, " +
		         "taskName VARCHAR(20) NOT NULL, " +
		         "notes VARCHAR(20) , " +       
		         "dueDate DATE)";
	
				dbAccess     = new DBAccess(this, "todolist.dat", sql);
				
				Bean record = new Bean(taskName, details, dueDate);
				dbAccess.insertData(record);				
				
				Toast t = Toast.makeText(this, "task entered", Toast.LENGTH_LONG);
				t.show();
				finish();
			}
			else {
				Toast t = Toast.makeText(this, "Please enter a name for your task!", Toast.LENGTH_LONG);
				t.show();
			}
		}
		
	}
}
